package com.han.hotelplat.service.wallet.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.base.Pager;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.comm.utils.StrUtil;
import com.han.hotelplat.dao.wallet.WalletDao;
import com.han.hotelplat.dao.walletoverdraft.WalletoverdraftDao;
import com.han.hotelplat.pojo.employee.Employee;
import com.han.hotelplat.pojo.hotel.Hotel;
import com.han.hotelplat.pojo.wallet.Wallet;
import com.han.hotelplat.pojo.walletoverdraft.Walletoverdraft;
import com.han.hotelplat.service.childtask.ChildTaskService;
import com.han.hotelplat.service.wallet.WalletService;

@Service
public class WalletSericeImpl implements WalletService{
	@Autowired
	private WalletDao walletDao;
	@Autowired
	private WalletoverdraftDao walletoverdraftDao;
	@Autowired
	private EntityManage entityManage;
	@Autowired
	private ChildTaskService childTaskService;
	
	@Override
	public Wallet getWallet(String userid) {
		List<Wallet> li = walletDao.findByProperty("userid", userid);
		if(li.size() != 0){
			return li.get(0);
		}
		return null;
	}

	@Override
	public Map<String, Object> setWalletPayPassword(String userid, String password) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Wallet> li = walletDao.findByProperty("userid", userid);
		Wallet wallet = null;
		if(li.size() != 0){
			wallet  = li.get(0);
		}
		
		if(wallet != null){
			//修改状态为已激活
			if(wallet.getState().intValue() == Constant.WALLET_STATE_NOTACTIVE){
				wallet.setState(Constant.WALLET_STATE_ACTIVE);
			}
			walletDao.setPaypassword(wallet, password);
			result.put("code", 0);
			result.put("message", "密码修改成功");
			result.put("wallet", wallet);
			return result;
		}
		result.put("code", -1);
		result.put("message", "钱包不存在");
		return result;
	}

	@Override
	public void save(Wallet wallet) {
		walletDao.save(wallet);		
	}

	@Override
	public void updateHoteOverdraft(float parseFloat,Wallet wallet, String optuserid, String remark) {
		wallet.setOverdraft(parseFloat);
		walletDao.update(wallet);
		
		Walletoverdraft wf =new Walletoverdraft();
		wf.setCreattime(new Date());
		wf.setMoney(parseFloat);//此次修改的金额
		wf.setOptuserid(optuserid);
		wf.setOverdraft(parseFloat);//修改后的授信额度
		wf.setRemark(wf.getRemark()+";"+remark);
		wf.setWalletid(wallet.getId());
		walletoverdraftDao.save(wf);
	}

	@Override
	public void update(Wallet w) {
		walletDao.update(w);
	}

	@Override
	public List<Hotel> exportHoteBalance(String startDate, String endDate,
			Integer state, Integer sortType, Integer areaCode,
			String condition, Integer isArrears) {
		StringBuffer sbf = new StringBuffer( " from  hotel.tb_hotel_manager hm LEFT JOIN hotel.tb_hotel h ON "
				+ " h.userid=hm.userid , hotel.tb_wallet w   where 1=1 and w.userid=h.id " );
		if(startDate != null && !startDate.isEmpty()){
			sbf.append(" and h.creattime >= '"+startDate+" 00:00:00'");
		}
		if(endDate != null && !endDate.isEmpty()){
			sbf.append(" and h.creattime <= '"+endDate+" 23:59:59'");
		}
		if(state != null  && state.intValue() != 0){
			sbf.append(" and h.state ="+state);
		}
		if(condition != null && !condition.isEmpty()){
			sbf.append("and (h.hotelname like '%"+condition+"%'"
					+ " or hm.tel like '%"+condition+"%'"
					+" or hm.realname like '%"+condition+"%')");
		}
		
		if(areaCode != null && areaCode.intValue()>100000){//区域码至少6位
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(h.area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(h.area,signed) - "+areaCode +" <= "+x;
			sbf.append(w_area);
		}
		
		if(isArrears != null && isArrears.intValue() == 1){//有欠款
			sbf.append(" and w.balance<0 ");
		}
		
		if(isArrears != null && isArrears.intValue() == 2){//无欠款
			sbf.append(" and w.balance>=0 ");
		}
		sbf.insert(0, " select h.id as id, h.hotelname as hotelname,h.area as area, h.hoteltype as hoteltype, "
				+ "hm.userid as userid, hm.realname as realname, hm.tel as tel, h.creattime as creattime, "
				+ "h.state as state, w.balance as balance, w.overdraft as overdraft  ");
		
		List<Hotel> data = new ArrayList<Hotel>();
		if(sortType != null && sortType.intValue() == 1){
			sbf.insert(sbf.length(), " order by w.balance desc");
		}else{
			sbf.insert(sbf.length(), " order by h.creattime desc");
		}
		List li = entityManage.findBySql(sbf.toString());
		for(int i= 0 ; i<li.size(); i++){
			Object[] ory = (Object[]) li.get(i);
			Hotel h = new Hotel();
			if(ory[0] != null){
				h.setId(Integer.parseInt(ory[0].toString()));
			}
			if(ory[1] != null){
				h.setHotelname(ory[1].toString());
			}
			if(ory[2] != null){
				if(ory[2] != null){
					h.setArea(Integer.parseInt(ory[2].toString()));
					h.setAreaModel(AreaHelp.getInstance().getAreaModels(h.getArea().toString()));
				}
			}
			if(ory[3] != null){
				h.setHoteltype(Integer.parseInt(ory[3].toString()));
			}
			if(ory[4] != null){
				h.setUserid(ory[4].toString());
			}
			if(ory[5] != null){
				h.setRealname(ory[5].toString());
			}
			if(ory[6] != null){
				h.setTel(ory[6].toString());
			}
			if(ory[7] != null){
				h.setCreattime(DateUtil.parseYMDHMS(ory[7].toString()));
			}
			if(ory[8] != null){
				h.setState(Integer.parseInt(ory[8].toString()));
			}
			if(ory[9] != null){
				h.setBalance(Float.valueOf(ory[9].toString()));
			}
			if(ory[10] != null){
				h.setOverdraft(Float.valueOf(ory[10].toString()));
			}
			data.add(h);
		}
		return data;
	}

	@Override
	public List<Employee> expEmpployyerBalance(String startDate,
			String endDate, Integer areaCode, String condition, Integer sortType) {
		StringBuffer hql = new StringBuffer("select new Employee (em.id, em.userid,em.realname,em.sex,"
				+ "em.workyears,em.area,em.state,em.tel,w.balance, em.idcard,em.bankcard) from Employee as em, Wallet w where w.userid=em.userid ");
		if(condition!=null && !condition.isEmpty()){
			hql.append(" and (realname like '%"+condition+"%'"
					+ " or tel like '%"+condition+"%'"
					+ " or introduceriname like '%"+condition+"%'"
					+ " or emergencytel like '%"+condition+"%')");
		}
		if(areaCode != null && areaCode.intValue()>100000){
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(area,signed) - "+areaCode +" <= "+x;
			hql.append(w_area);
		}
		if(startDate != null && !startDate.isEmpty()){
			hql.append(" and em.creattime>='"+startDate+" 00:00:00'");
		}			  
		if(endDate != null && !endDate.isEmpty()){
			hql.append(" and em.creattime<='"+endDate+" 23:59:59'");
		}	
		if(sortType != null && sortType.intValue() == 1){
			hql.append(" order by w.balance desc");
		}else{
			hql.append(" order by em.creattime desc");
		}
		List<Employee> data = entityManage.findByHql(hql.toString());
		for (Employee em : data) {
			Float roomcount = childTaskService.getEmRoom(em.getUserid());
			em.setRoomcount(roomcount);
			if(!StrUtil.isNullOrEmpty(em.getArea())){
				em.setAreaModel(AreaHelp.getInstance().getAreaModels(em.getArea()));
			}
		}
		return data;
	}

}
